package com.ck.util;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ProfileUtil {


    private static AtomicInteger counter = new AtomicInteger(0);

    /**
     *      * 长生消息id
     *     
     */

    public static long getAtomicCounter() {
        if (counter.get() > 999999) {
            counter.set(1);
        }
        long time = System.currentTimeMillis();
        long returnValue = time * 100 + counter.incrementAndGet();
        return returnValue;
    }


    public static void main(String[] args) {
        Random random = new Random();
        System.out.println(random.nextDouble());
        //System.out.println(ProfileUtil.getAtomicCounter());
        for(int i=0;i<10000;i++){
            createRandomInteger();
        }
        //System.out.println(getID());


    }

    public static List<Integer> list() {
        List<Integer> list = new ArrayList<Integer>(); //创建空集合
        Random random = new Random();
        int rand = 0;
        while (true) {
            rand = random.nextInt(10) + 1; //生成 一个数字 1-10
            if (list.size() == 0) { //初始化，给list的第一个元素 添加数据 (由于涉及到list.size(),list至少有一个元素,否则跳过for循环)
                list.add(rand);
            } else {  //第一个元素以后添加数据
                for (int i = 0; i < list.size(); i++) {
                    int count = 0; //计数重复数字的个数
                    for (int j = 0; j < list.size(); j++) {
                        if (rand == list.get(j)) {
                            count++;
                        }
                    }
                    if (count == 0) {//重复数字的个数等于0时，执行添加方法
                        list.add(rand);
                    }
                }
                if (list.size() >= 10) { //list元素的个数达到10个 就终止while
                    break;
                }
            }
        }
        return list;
    }

    private static final long LIMIT = 10000000000L;
    private static long last = 0;

    public static long getID() {
        // 10 digits.
        long id = System.currentTimeMillis() % LIMIT;
        if ( id <= last ) {
            id = (last + 1) % LIMIT;
        }
        return last = id;
    }

    private static Set<Long> set = new HashSet<>();
    private static void createRandomInteger(){
        Random aRandom = new Random();
        int aStart = 1000000000;
        long aEnd = 9999999999L;
        //获取范围，铸造为长以避免溢出问题
        long range = aEnd  - (long)aStart + 1;
        //System.out.println("范围:"+range);
        //计算范围的一小部分，0< = frac< range
        //System.out.println("random:"+aRandom.nextDouble());
        long fraction =(long)(range * aRandom.nextDouble());
        //System.out.println("fraction:"+fraction);
        long randomNumber = fraction +(long)aStart;
        System.out.println(randomNumber);
        if(!set.contains(randomNumber)){
            set.add(randomNumber);
        }else{
            System.out.println("重复:"+randomNumber);
        }
    }
}
